//
//  Message.mm
//  WCDB_Demo
//
//  Created by lxj on 2018/7/25.
//  Copyright © 2018年 lxj. All rights reserved.
//

#import "Message+WCTTableCoding.h"
#import "Message.h"
#import <WCDB/WCDB.h>

#import "DataBaseManager.h"
@implementation Message

WCDB_IMPLEMENTATION(Message)
WCDB_SYNTHESIZE(Message, userId)
WCDB_SYNTHESIZE(Message, remarkname)
WCDB_SYNTHESIZE(Message, username)

WCDB_SYNTHESIZE(Message, headImageUrl)
 
///自定义名称
//WCDB_SYNTHESIZE_COLUMN(Message, time, "date")
///默认值
//WCDB_SYNTHESIZE_DEFAULT(Message, age, 1)
WCDB_PRIMARY(Message, userId)
//WCDB_UNIQUE(Message, name)
//WCDB_NOT_NULL(Message, name)
//WCDB_INDEX(Message, "_idx1", createTime)
///FTS
WCDB_VIRTUAL_TABLE_MODULE(Message, @"fts5")
WCDB_VIRTUAL_TABLE_TOKENIZE(Message, @"WCDB")
//WCTTokenizerNameWCDB
//WCTTokenizerNameApple

+ (BOOL)insertObject:(Message *)object{
    WCTDatabase *dataBase = [DataBaseManager sharedInstance].dataBase;
//    object.isAutoIncrement = YES;

//    [self insertObjectFTS:object];
    
    return [dataBase insertOrReplaceObject:object into:TABLE_WCDB_NAME];
}

+ (BOOL)insertObjects:(NSArray<Message *> *)array useTransaction:(BOOL)useTransaction{
    WCTDatabase *dataBase = [DataBaseManager sharedInstance].dataBase;

    if (useTransaction) {
        BOOL result = [dataBase beginTransaction];
        if (!result) {
            NSLog(@"事务开启失败");
        }
        //批量插入数据
        [dataBase insertObjects:array into:TABLE_WCDB_NAME];

        if (![dataBase commitTransaction]) {///事务失败 回滚
            [dataBase rollbackTransaction];
        }

        return YES;
    }else{
        return [dataBase insertObjects:array into:TABLE_WCDB_NAME];
    }
}


+ (NSArray<Message *> *)getAllObjects{
    WCTDatabase *dataBase = [DataBaseManager sharedInstance].dataBase;
    WCTSelect *select = [dataBase prepareSelectObjectsOfClass:Message.class fromTable:TABLE_WCDB_NAME];
    [select setStatisticsEnabled:YES];

    NSArray<Message *> *array = select.allObjects;
    NSLog(@"耗时%f %lu条数据", select.cost,(unsigned long)array.count);
    return array;
}

+ (Message *)getOneObjectByUserID:(NSString *)userID{
    WCTDatabase *dataBase = [DataBaseManager sharedInstance].dataBase;
    Message *message = [dataBase getOneObjectOfClass:self fromTable:TABLE_WCDB_NAME where:Message.userId == userID] ;
    return message;
}
///部分查询示例代码
+ (NSArray<Message *> *)getAllObjectsq{
//    WCTDatabase *dataBase = [DataBaseManager sharedInstance].dataBase;
//    return [dataBase getObjectsOnResults:{Message.core,Message.cores} fromTable:TABLE_WCDB_NAME where:Message.core > 4 ||  Message.name.like("李晓杰%")];
    return @[];
}

+ (BOOL)updateAllRowsWithObject:(Message *)message{
    WCTDatabase *dataBase = [DataBaseManager sharedInstance].dataBase;
    
    return YES;
}
+ (BOOL)deleteAllObjects{
    WCTDatabase *dataBase = [DataBaseManager sharedInstance].dataBase;
    [dataBase deleteAllObjectsFromTable:TABLE_WCDB_NAME];
//    [dataBase deleteAllObjectsFromTable:@"MessageFTS"];
    return YES;
}
+ (BOOL)deleteByUserId:(NSString *)userId{
    WCTDatabase *dataBase = [DataBaseManager sharedInstance].dataBase;
    return [dataBase deleteObjectsFromTable:TABLE_WCDB_NAME where:Message.userId == userId];
}
#pragma mark - FTS
+ (BOOL)insertObjectFTS:(Message *)object{
    WCTDatabase *dataBase = [DataBaseManager sharedInstance].dataBase;
//    object.isAutoIncrement = YES;
    BOOL result = [dataBase insertObject:object into:@"MessageFTS"];
    return result;
}
+ (NSArray<Message *> *)getObjectsFTSByText:(NSString *)text{
    ///FTS查询
    WCTDatabase *dataBase = [DataBaseManager sharedInstance].dataBase;
    WCTSelect *select = [dataBase prepareSelectObjectsOfClass:Message.class fromTable:@"MessageFTS"];
    [select setStatisticsEnabled:YES];

    NSString *searchText = [NSString stringWithFormat:@"%@*",text];
    NSArray<Message *> *array = [select where:Message.name.match(searchText)].allObjects;
    NSLog(@"耗时%f %lu条数据", select.cost,(unsigned long)array.count);
    ///like查询
//    WCTDatabase *dataBase = [DataBaseManager sharedInstance].dataBase;
//    WCTSelect *select = [dataBase prepareSelectObjectsOfClass:Message.class fromTable:TABLE_WCDB_NAME];
//    [select setStatisticsEnabled:YES];
//
//    NSString *searchText = [NSString stringWithFormat:@"%%%@%%",text];
//    NSArray<Message *> *array = [select where:Message.name.like(searchText)].allObjects;
//    NSLog(@"耗时%f %lu条数据", select.cost,(unsigned long)array.count);

    return array;
}
+ (BOOL)deleteFTSByUserId:(NSInteger)userId{
    WCTDatabase *dataBase = [DataBaseManager sharedInstance].dataBase;
    return [dataBase deleteObjectsFromTable:@"MessageFTS" where:Message.userId == userId];
}

#pragma mark - 李晓杰添加 打印看到属性
// 重写debugDescription, 而不是description
- (NSString *)debugDescription {
    //声明一个字典
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

    //得到当前class的所有属性
    uint count;
    objc_property_t *properties = class_copyPropertyList([self class], &count);

    //循环并用KVC得到每个属性的值
    for (int i = 0; i<count; i++) {
        objc_property_t property = properties[i];
        NSString *name = @(property_getName(property));
        id value = [self valueForKey:name]?:@"nil";//默认值为nil字符串
        [dictionary setObject:value forKey:name];//装载到字典里
    }
    //释放
    free(properties);
    //return
    return [NSString stringWithFormat:@"<%@: %p> -- %@",[self class],self,dictionary];
}

@end
